<template>
  <view class="container">
    <TopBtn :title="'开具处方'"></TopBtn>
    <view class="info">
      <patientCard :data="patientdata"></patientCard>
      <view class="warning currency_card mt_30">
        <view class="currency_title flex-center">处方信息</view>
      </view>
      <view class="currency_card">
        <view class="CARDtitle flex-sp-center" @click="goinfo('pages/CommonTools/ChooseADisease/ChooseADisease')">
          <view class="">
            <text>*</text>
            疾病名称
            <!-- {{diseaseId}} -->
          </view>
          <!-- <RadioPicker v-model="data.diseaseId" :inputName="data.diseaseId" :Radio="'matchIncome'"
						:range="queryAllDiseaseList"></RadioPicker> -->
          <view class="uni-input flex-center">
            <text class="InputText" v-if="data.diseaseId">{{ names }}</text>
            <text v-else class="placeholder">请选择</text>
            <uni-icons type="right" color="#999" size="16"></uni-icons>
          </view>
        </view>
        <view class="CARDtitle">
          <text>*</text>
          诊断
        </view>
        <textArea v-model="data.diagnosis" :inputNane="data.diagnosis" :maxNum="30"></textArea>
        <view class="Prescriptiontitle mt_30 flex-center">
          <view class="">
            <text>*</text>
            RP
          </view>
          <view class="add flex-center-center" @click="goadd()">
            {{ showdrugName.length > 0 ? '修改处方' : '添加处方' }}
          </view>
        </view>
        <view class="text PrescriptionDetails" v-if="showdrugName.length > 0">
          <view class="PrescriptionList">
            <view class="" v-for="(n, index) in showdrugName">
              {{ n.name }}{{ n.dosage }}
              <text v-if="n.DecoctionMethod">({{ n.DecoctionMethod }})</text>
            </view>
          </view>
          <view class="row" v-if="showapply.dose">
            <text>用法：</text>
            共{{ showapply.dose }}剂，{{ showapply.operationMethod }}，{{ showapply.dosageNum }}，{{
      showapply.usageCount
    }}，{{ showapply.perUse }}
          </view>
        </view>

        <view class="CARDtitle">加工</view>
        <textArea v-model="data.machining" :inputNane="data.machining" :maxNum="30"></textArea>
        <view class="CARDtitle mt_30">医嘱</view>
        <textArea v-model="data.medicalAdvice" :inputNane="data.medicalAdvice" :maxNum="100"></textArea>
      </view>
      <view class="warning currency_card pb_37">
        <view class="title flex-center">
          <image src="@/static/CommonTools/warning.png" mode=""></image>
          注意事项
        </view>
        <view class="text">
          1、开处方前请确认患者的肝肾功能是否正常；
          <br />
          2、为了降低医疗风险，如果您开具的药品对肝肾功能有潜在影响，建议您像患者说明并提醒患者定期复查肝肾功能。
          <br />
          3、依据国家《处方管理办法》，门诊处方最长三日有效，请您提醒患者及时购药。
        </view>
      </view>
    </view>
    <view class="kong"></view>
    <view class="CommonToolsbtn flex-center-center" @click="sub()">预览处方</view>
  </view>
</template>

<script>
import {
  SendGroupMessage
} from '@/utils/TIM'
import {
  getPatientInfoByOutId,
  createPrescriptionInfo,
  getPrescriptionInfo,
  queryAllDisease,
} from '@/api/ConsultationRoom'
import {
  getDiseaseControlList
} from '@/api/user'
export default {
  data () {
    return {
      groupId: '',
      id: '',
      data: {
        diagnosis: '',
      },
      refuseReason: '',
      showapply: [],
      showdrugName: [],
      patientdata: {},
      type: 1,
      queryAllDiseaseList: [],
      names: '',
    }
  },
  onShow () {
    let that = this
    uni.$once('ids', function (data) {
      this.$set(that.data, 'diseaseId', data)
      that.getDiseaseControlList()
    })
    uni.$on('baseFormData', function (data) {
      console.log('处方数据：', data)
      that.data = {
        ...that.data,
        ...data,
      }
      that.showdrugName = JSON.parse(data.drugName)
      that.showapply = JSON.parse(data.apply)
    })
  },
  onLoad (options) {
    this.id = options.id
    this.groupId = options.groupId
    if (options.type) {
      this.type = options.type
    }
    this.queryAllDisease()
    this.getPatientInfoByOutId()
    if (options.updata && options.updata != 0) {
      this.getPrescriptionInfo(options.updata)
    }
  },
  methods: {
    getDiseaseControlList () {
      getDiseaseControlList({
        ids: this.data.diseaseId,
      }).then(res => {
        console.log(res)
        var name = []
        res.data.forEach(item => {
          name.push(item.name)
        })

        this.names = name.join(',')
      })
    },
    queryAllDisease () {
      queryAllDisease().then(res => {
        this.queryAllDiseaseList = res.data
      })
    },
    getPrescriptionInfo (e) {
      getPrescriptionInfo({
        id: e,
      }).then(res => {
        this.data = res.data
        this.showdrugName = JSON.parse(res.data.drugName)
        this.showapply = JSON.parse(res.data.apply)
      })
    },
    sub () {
      this.data.source = this.type
      if (this.type == 1) {
        this.data.outpatientId = this.id
      } else if (this.type == 2) {
        this.data.onlinePreFormId = this.id
      }
      this.data.patientId = this.patientdata.patientId
      this.data.departName = this.patientdata.departName
      this.data.patientAge = this.patientdata.patientAge
      this.data.patientIdCard = this.patientdata.patientIdCard
      this.data.patientName = this.patientdata.patientName
      this.data.patientSex = this.patientdata.patientSex
      this.data.weight = this.patientdata.weight
      if (!this.data.diagnosis) {
        uni.showToast({
          title: '请填写诊断',
          duration: 2000,
          icon: 'none',
        })
        return
      }
      if (!this.data.drugName) {
        uni.showToast({
          title: '请填写处方',
          duration: 2000,
          icon: 'none',
        })
        return
      }
      uni.setStorage({
        key: 'paramsData',
        data: JSON.stringify(this.data),
        success: function () {
          uni.navigateTo({
            url: '/pages/ConsultationRoom/PrescriptionDetails/PrescriptionDetails?type=2',
          })
        },
      })

      return
      // uni.showLoading({
      // 	title: '加载中',
      // 	mask: true
      // })
      // createPrescriptionInfo(this.data).then(res => {
      // 	this.patientdata = res.data
      // 	uni.showToast({
      // 		title: '操作成功',
      // 		duration: 200,
      // 		icon: 'none',
      // 	})
      // 	setTimeout(() => {
      // 		uni.hideLoading()
      // 		uni.$emit('show', true)
      // 		uni.navigateBack({
      // 			delta: 1, //返回层数，2则上上页
      // 		})
      // 	}, 200); // 3000毫秒后执行
      // })
    },
    getPatientInfoByOutId () {
      var id = {}
      if (this.type == 1) {
        id.outpatientId = this.id
      } else if (this.type == 2) {
        id.onlinePreFormId = this.id
      }
      getPatientInfoByOutId(id).then(res => {
        this.patientdata = res.data
      })
    },
    goadd () {
      this.data.groupId = this.groupId
      this.data.outPatientId = this.id
      this.data.patientId = this.patientdata.patientId
      this.data.departName = this.patientdata.departName
      this.data.patientAge = this.patientdata.patientAge
      this.data.patientIdCard = this.patientdata.patientIdCard
      this.data.patientName = this.patientdata.patientName
      this.data.patientSex = this.patientdata.patientSex
      this.data.weight = this.patientdata.weight
      this.data.height = this.patientdata.height
      uni.setStorage({
        key: 'paramsData',
        data: JSON.stringify(this.data),
        success: function () {
          uni.navigateTo({
            url: `/pages/prescription/AddPrescription/AddPrescription?type=3`,
          })
        },
      })
    },
    goinfo (e, id) {
      if (id) {
        uni.navigateTo({
          url: `/${e}?id=${id}`,
        })
      } else {
        uni.navigateTo({
          url: `/${e}`,
        })
      }
    },
  },
}
</script>

<style lang="less" scoped>
.uni-input {
  display: flex;
  justify-content: right;
  align-items: center;
  height: 100%;
  height: 70rpx;
  font-size: 30rpx;
  font-family: PingFang SC;
  font-weight: 500;

  .placeholder {
    color: #999 !important;
  }

  .InputText {
    width: 400rpx;
    color: #666666 !important;
    font-size: 26rpx;
  }
}

.PrescriptionDetails {
  border-top: 1px solid #eeeeee;
  padding-top: 15rpx;
  font-weight: 500;
  font-size: 28rpx;
  color: #333333;

  .PrescriptionList {
    display: flex;
    flex-wrap: wrap;
    justify-content: space-between;

    &>view {
      width: 48%;
      margin-bottom: 15rpx;
    }
  }

  .row {
    display: flex;

    text {
      width: 100rpx;
    }
  }
}

.info {
  padding: 30rpx;
  box-sizing: border-box;
}

.currency_card {
  .CARDtitle {
    font-weight: 500;
    font-size: 28rpx;
    color: #666666;
    padding: 10rpx 0 30rpx;

    text {
      color: #ff0303;
      margin-right: 10rpx;
    }
  }

  .Prescriptiontitle {
    font-weight: 500;
    font-size: 28rpx;
    color: #666666;
    padding: 10rpx 0 20rpx;

    .add {
      width: 138rpx;
      height: 60rpx;
      border-radius: 5rpx;
      border: 1px solid #3e6efe;
      font-weight: 500;
      font-size: 28rpx;
      color: #3e6efe;
      margin-left: 22rpx;
    }
  }
}

.warning {
  image {
    width: 30rpx;
    height: 30rpx;
    margin-right: 15rpx;
  }

  font-weight: 500;
  font-size: 28rpx;
  color: #333333;
  background-color: #fff0;

  .text {
    color: #999;
  }
}
</style>